import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/api/api_common.dart';
import 'package:flutter_book/data/model/yck/yck_rule_book_3_model.dart';
import 'package:flutter_book/util/analyze/analyze_yck_rule_book_util.dart';
import 'package:flutter_book/widget/common/custom_appbar.dart';

class SourceAnalyzePage extends StatelessWidget {
  final YckRuleBook3Model yckRuleModel;

  SourceAnalyzePage(this.yckRuleModel);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleString: yckRuleModel.bookSourceName,
        leadingOnTap: () {
          Navigator.pop(context);
        },
      ),
      body: _body(),
    );
  }

  Widget _body() {
    //https://m.liewen.la/search.php?q=%E6%96%97%E7%BD%97%E5%A4%A7%E9%99%86&p=1
    // AnalyzeYckRuleBookUtil.init().analyzeHtml(AnalyzeYckRuleBookUtil.htmlString, yckRuleModel);
    // return Container();
    return Container(
      child: Column(
        children: [
          Flexible(
              child: Container(
                color: Colors.green,
                child: SingleChildScrollView(
                  child: FutureBuilder(
                    future: ApiCommon.requestGetUrl(
                        yckRuleModel.searchUrl?.replaceAll("{{key}}", "斗罗大陆").replaceAll("{{page}}", "0") ?? ""),
                    builder: (context, AsyncSnapshot<String?> snapshot) {
                      if (snapshot.connectionState == ConnectionState.done) {
                        if (snapshot.hasError) {
                          return Container(child: Text(snapshot.error.toString()));
                        }
                        if (snapshot.data == null || snapshot.data!.length < 1) {
                          return Container(child: Text("暂无数据"));
                        }
                        AnalyzeYckRuleBookUtil.htmlString = snapshot.data!;

                        // AnalyzeHtmlUtil.init().getMenuUrlList(snapshot.data!);
                        return Text(snapshot.data!);
                      }
                      return Container(child: Text("正在加载"));
                    },
                  ),
                ),
              ),
              flex: 1),
          Flexible(
              child: Container(
                color: Colors.blueAccent,
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(yckRuleModel.ruleSearch?.toJson().toString() ?? ""),
                    ],
                  ),
                ),
              ),
              flex: 1),
        ],
      ),
    );
  }
}
